<template>
  <div class="relative_height purchase_wrap">
    <Navbar :isBack="true" />
    <div class="top">
      <div class="pageWidth top_cont u-flex u-row-between">
        <div>
          <div class="top_tit">采购中心</div>
          <div class="top_eng">助您深入了解市场，精准选品，轻松打造爆款</div>
        </div>
        <img src="../../image/mine/purIcon.png" class="top_img" />
      </div>
    </div>
    <div class="shop pageWidth u-flex u-row-between u-col-top">
      <div class="shop_left u-flex u-flex-wrap">
        <div class="left_item hand" v-for="(item, index) in shopList" :key="index" v-if="total > 0">
          <div class="item_img" @click="showPreview(item.goods_image)">
            <img :src="item.goods_image" class="img_big" />
            <div class="img_pre u-flex-center hand">
              <img src="../../image/mine/magnifier.png" alt="" />
            </div>
          </div>
          <div class="item_cont" @click="goDetail(item)">
            <div class="item_top">
              <div class="item_tit webkit-line-clamp-one">{{ item.goods_name }}</div>
              <div class="item_bot u-flex u-row-between">
                <div class="self_support" v-if="item.store_user_id == 0">自营</div>
                <div
                  class="u-flex cai_text hand"
                  :class="{ purchased: purchasedItems.has(item.goods_id) }"
                  @click="!purchasedItems.has(item.goods_id) && addCart(item)"
                >
                  <img src="../../image/mine/shop.png" class="cai_img" />
                  采购
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="no_data u-flex-center" v-else>暂无数据</div>
      </div>
      <div class="shop_right">
        <div class="rig_tit">全部分类</div>
        <div
          class="rig_item u-flex hand"
          :class="{ active: classIndex == index }"
          v-for="(item, index) in classifyList"
          :key="index"
          @click="classifyClick(index)"
        >
          <img src="../../image/mine/all.png" v-if="index == 0" />
          <img src="../../image/mine/self.png" v-if="index == 1" />
          <img :src="item.image.external_url" v-if="index > 1" />
          <div class="item_tit">{{ item.name }}</div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <div class="page_box">
      <Pagination
        v-model:currentPage="currentPage"
        :pageSize="pageSize"
        :total="total"
        :maxVisible="5"
        v-if="total > pageSize"
        @page-change="handlePageChange"
      />
    </div>

    <!-- 图片预览 -->
    <div
      v-if="previewVisible"
      class="image-preview-overlay"
      @click="closePreview"
      @touchmove="preventTouchMove"
    >
      <div class="image-preview-content" @click.stop>
        <img :src="previewImage" :style="previewStyle" class="preview-image" />
        <div class="close-btn" @click="closePreview">×</div>
      </div>
    </div>

    <!-- 成功加入采购清单 -->
    <el-dialog
      v-model="successShow"
      :modal="false"
      :show-close="false"
      :close-on-click-modal="false"
      align-center
    >
      <div class="success_box u-flex-col u-col-center u-row-center">
        <img src="../../image/mine/signArrow.png" alt="" class="sign_img" />
        <div class="tit">成功加入采购清单</div>
        <div class="suc_btn u-flex-center hand" @click="router.go(-1)">去采购清单</div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
  import { useStore } from 'vuex'
  import { ref, onMounted, watch, computed, getCurrentInstance, reactive, nextTick } from 'vue'
  import { useRouter, useRoute } from 'vue-router'

  const appContext = getCurrentInstance().appContext
  const imgUrl = appContext.config.globalProperties.$imgUrl
  const store = useStore()
  const router = useRouter()

  const classifyList = ref([])
  const classIndex = ref(localStorage.getItem('classIndex') || 0)
  const storeGoods = ref(localStorage.getItem('storeGoods') || '') //自营商品0  非自营商品 ''

  const shopList = ref([])
  const currentPage = ref(localStorage.getItem('currentPage') || 1)
  const pageSize = ref(15)
  const total = ref(1)

  const successShow = ref(false)
  // 存储已采购的商品ID，用于控制按钮状态
  const purchasedItems = ref(new Set())

  // 图片预览相关
  const previewVisible = ref(false)
  const previewImage = ref('')
  const previewStyle = ref({
    width: '80%',
    height: '80%',
    objectFit: 'contain',
  })

  onMounted(() => {
    categoryFn()
  })

  // 图片预览方法
  const showPreview = imageSrc => {
    previewImage.value = imageSrc
    previewVisible.value = true
    // 禁止页面滚动
    document.body.style.overflow = 'hidden'
    document.addEventListener('touchmove', preventTouchMove, { passive: false })
  }

  // 关闭预览
  const closePreview = () => {
    previewVisible.value = false
    // 恢复页面滚动
    document.body.style.overflow = ''
    document.removeEventListener('touchmove', preventTouchMove)
  }

  // 阻止触摸移动事件
  const preventTouchMove = event => {
    event.preventDefault()
  }

  // 分类列表
  const categoryFn = () => {
    store.dispatch('home/categoryListApi').then(res => {
      if (res.status == 200) {
        // 在数组开头添加"全部"分类
        classifyList.value = [
          {
            name: '全部',
            category_id: '',
          },
          {
            name: '自营产品',
            category_id: '',
          },
          ...res.data.list,
        ]

        goodsFn() // 商品列表
      }
    })
  }

  // 商品列表
  const goodsFn = () => {
    store
      .dispatch('home/goodsListApi', {
        page: currentPage.value,
        per_page: pageSize.value,
        categoryId: classifyList.value[classIndex.value].category_id,
        storeGoods: storeGoods.value,
      })
      .then(res => {
        if (res.status == 200) {
          shopList.value = res.data.list.data
          total.value = res.data.list.total
        }
      })
  }

  const classifyClick = index => {
    if (index == 1) {
      storeGoods.value = 0
    } else {
      storeGoods.value = ''
    }
    classIndex.value = index
    currentPage.value = 1
    localStorage.setItem('classIndex', classIndex.value)
    localStorage.setItem('storeGoods', storeGoods.value)
    localStorage.setItem('currentPage', currentPage.value)

    goodsFn() // 商品列表
  }

  // 分页变化处理
  const handlePageChange = page => {
    currentPage.value = page

    localStorage.setItem('currentPage', currentPage.value)
    goodsFn() // 商品列表
  }

  // 加入购物车
  const addCart = item => {
    store
      .dispatch('home/addCartApi', {
        goods_id: item.goods_id,
      })
      .then(res => {
        if (res.status == 200) {
          // 将商品ID添加到已采购集合中
          purchasedItems.value.add(item.goods_id)
          successShow.value = true
          setTimeout(() => {
            successShow.value = false
          }, 1500)
        }
      })
  }

  // 跳商品详情
  const goDetail = item => {
    localStorage.setItem('classIndex', classIndex.value)
    localStorage.setItem('currentPage', currentPage.value)
    router.push('/shopDetail?id=' + item.goods_id)
  }
</script>

<style scoped lang="less">
  // 兼容
  @media screen and (max-width: 1920px) {
    .purchase_wrap {
      // 头图
      .top {
        margin-bottom: 32px !important;

        .top_cont {
          .top_tit {
            font-size: 34px !important;
            margin-bottom: 25px !important;
          }

          .top_eng {
            font-size: 15px !important;
          }
          .top_img {
            width: 500px !important;
          }
        }
      }

      .shop {
        .shop_left {
          margin-right: 30px !important;

          .left_item {
            margin-bottom: 11px !important;

            .item_cont {
              padding: 9px 15px !important;
              font-size: 13px !important;

              .item_tit {
                font-size: 13px !important;
              }

              .item_bot {
                margin-top: 12px !important;
              }

              .cai_text {
                .cai_img {
                  width: 18px !important;
                  margin-right: 4px !important;
                }

                &.purchased {
                  color: #999 !important;
                  opacity: 0.6 !important;

                  .cai_img {
                    filter: grayscale(100%) !important;
                  }
                }
              }

              .self_support {
                padding: 2px 5px !important;
              }
            }
          }
        }

        .no_data {
          height: 460px !important;
        }

        .shop_right {
          width: 195px !important;
          padding: 0 13px !important;

          .rig_tit {
            font-size: 13px !important;
            padding: 13px 0 !important;
          }

          .rig_item {
            height: 24px !important;
            padding-left: 18px !important;
            font-size: 13px !important;
            margin-bottom: 5.5px !important;

            img {
              width: 15px !important;
              margin-right: 9px !important;
            }
          }
        }
      }

      .page_box {
        padding-bottom: 35px !important;
      }
    }

    // 成功弹窗适配
    .success_box {
      width: 230px !important;
      height: 225px !important;
      font-size: 14px !important;

      img {
        width: 40px !important;
        margin-bottom: 18px !important;
      }

      .suc_btn {
        width: 120px !important;
        height: 36px !important;
        font-size: 12px !important;
        margin-top: 28px !important;
        border-radius: 7px !important;
      }
    }
  }

  @media screen and (max-width: 1700px) {
    .purchase_wrap {
      // 头图
      .top {
        margin-bottom: 28px !important;

        .top_cont {
          .top_tit {
            font-size: 30px !important;
            margin-bottom: 22px !important;
          }

          .top_eng {
            font-size: 14px !important;
          }
          .top_img {
            width: 450px !important;
          }
        }
      }

      .shop {
        .shop_left {
          margin-right: 28px !important;

          .left_item {
            margin-bottom: 10px !important;

            .img_pre {
              width: 25px !important;
              height: 25px !important;
            }

            .item_cont {
              padding: 8px 14px !important;
              font-size: 12px !important;

              .item_tit {
                font-size: 12px !important;
              }

              .item_bot {
                margin-top: 10px !important;
              }

              .cai_text {
                .cai_img {
                  width: 17px !important;
                  margin-right: 4px !important;
                }

                &.purchased {
                  color: #999 !important;
                  opacity: 0.6 !important;

                  .cai_img {
                    filter: grayscale(100%) !important;
                  }
                }
              }

              .self_support {
                padding: 2px 5px !important;
                font-size: 11px !important;
              }
            }
          }
        }

        .no_data {
          height: 420px !important;
        }

        .shop_right {
          width: 185px !important;
          padding: 0 12px !important;

          .rig_tit {
            font-size: 12px !important;
            padding: 12px 0 !important;
          }

          .rig_item {
            height: 20px !important;
            padding-left: 16px !important;
            font-size: 12px !important;
            // margin-bottom: 5.5px !important;

            img {
              width: 14px !important;
              margin-right: 8px !important;
            }
          }
        }
      }

      .page_box {
        padding-bottom: 32px !important;
      }
    }

    // 成功弹窗适配
    .success_box {
      width: 220px !important;
      height: 215px !important;
      font-size: 13px !important;

      img {
        width: 36px !important;
        margin-bottom: 15px !important;
      }

      .suc_btn {
        width: 112px !important;
        height: 35px !important;
        font-size: 11px !important;
        margin-top: 23px !important;
      }
    }
  }

  @media screen and (max-width: 1500px) {
    .purchase_wrap {
      // 头图
      .top {
        margin-bottom: 25px !important;

        .top_cont {
          .top_tit {
            font-size: 28px !important;
            margin-bottom: 20px !important;
          }

          .top_eng {
            font-size: 14px !important;
          }
          .top_img {
            width: 380px !important;
          }
        }
      }

      .shop {
        .shop_left {
          margin-right: 25px !important;

          .left_item {
            width: 19.2% !important;
            margin-bottom: 10px !important;

            .item_cont {
              padding: 8px 12px !important;
              font-size: 12px !important;

              .item_tit {
                font-size: 12px !important;
              }

              .cai_text {
                .cai_img {
                  width: 16px !important;
                  margin-right: 4px !important;
                }

                &.purchased {
                  color: #999 !important;
                  opacity: 0.6 !important;

                  .cai_img {
                    filter: grayscale(100%) !important;
                  }
                }
              }

              .self_support {
                padding: 2px 4px !important;
                font-size: 10px !important;
                border-radius: 2px !important;
              }
            }
          }
        }

        .no_data {
          height: 380px !important;
          font-size: 12px !important;
        }

        .shop_right {
          width: 180px !important;
          padding: 0 12px !important;

          .rig_tit {
            font-size: 12px !important;
            padding: 12px 0 !important;
          }

          .rig_item {
            height: 20px !important;
            padding-left: 10px !important;
            font-size: 12px !important;

            img {
              width: 14px !important;
              margin-right: 8px !important;
            }
          }
        }
      }

      .page_box {
        padding-bottom: 30px !important;
      }
    }

    // 成功弹窗适配
    .success_box {
      width: 202px !important;
      height: 203px !important;
      font-size: 12px !important;

      img {
        width: 33px !important;
        margin-bottom: 15px !important;
      }

      .suc_btn {
        width: 105px !important;
        height: 33px !important;
        font-size: 11px !important;
        margin-top: 20px !important;
      }
    }
  }

  .purchase_wrap {
    // 头图
    .top {
      width: 100%;
      aspect-ratio: 16/2.6;
      margin-bottom: 37px;
      background: url('../../image/mine/purBg.png') no-repeat;
      background-size: 100% 100%;

      .top_cont {
        color: #fff;
        height: 100%;

        .top_tit {
          font-size: 38px;
          margin-bottom: 30px;
        }

        .top_eng {
          font-size: 16px;
        }
        .top_img {
          width: 615px;
          height: auto;
        }
      }
    }

    .shop {
      .shop_left {
        flex: 1;
        margin-right: 35px;

        .left_item {
          width: 19.2%;
          margin-right: 1%;
          border-radius: 14px;
          box-sizing: border-box;
          margin-bottom: 12px;
          border: 1px solid #eee;

          &:nth-child(5n) {
            margin-right: 0;
          }

          .item_img {
            width: 100%;
            aspect-ratio: 1/1;
            border-radius: 6px;
            overflow: hidden;
            position: relative;

            .img_pre {
              position: absolute;
              width: 30px;
              height: 30px;
              bottom: 0;
              right: 0;
              z-index: 2;
              border-radius: 2px;
              background: rgba(0, 0, 0, 0.3);

              img {
                width: 55%;
                height: auto;
              }
            }

            .img_big {
              width: 100%;
              height: 100%;
              object-fit: cover;
            }

            // :deep(.el-image) {
            //   width: 100%;
            //   height: 100%;

            //   .el-image__inner {
            //     width: 100%;
            //     height: 100%;
            //     object-fit: cover;
            //   }
            // }
          }

          .item_cont {
            padding: 10px 17px;
            font-size: 14px;

            .item_tit {
              font-size: 14px;
              color: #3d3d3d;
              font-weight: bold;
            }

            .item_bot {
              margin-top: 15px;
            }

            .cai_text {
              width: fit-content;
              color: #5463ff;
              cursor: pointer;
              transition: all 0.3s ease;

              .cai_img {
                width: 20px;
                height: auto;
                margin-right: 5px;
              }

              &.purchased {
                color: #999;
                opacity: 0.6;

                .cai_img {
                  filter: grayscale(100%);
                }
              }
            }

            .self_support {
              color: #fff;
              padding: 3px 6px;
              font-size: 12px;
              background: #5463ff;
              border-radius: 3px;
            }
          }
        }

        .left_item:hover {
          box-shadow: 0px 1px 12px 0px rgba(84, 99, 255, 0.3);
        }
      }

      .no_data {
        width: 100%;
        height: 500px;
        color: #888;
        font-size: 14px;
      }

      .shop_right {
        width: 208px;
        padding: 0 14px;
        border-radius: 13px;
        background: #f7f8fc;
        box-sizing: border-box;

        .rig_tit {
          font-size: 14px;
          font-weight: bold;
          color: #3d3d3d;
          padding: 14px 0;
        }

        .rig_item {
          height: 26px;
          border-radius: 7px;
          padding-left: 20px;
          font-size: 14px;
          color: #3d3d3d;
          margin-bottom: 6px;

          img {
            width: 16px;
            height: auto;
            margin-right: 10px;
          }

          &.active {
            background: #fff;
          }
        }
      }
    }

    .page_box {
      padding-bottom: 40px;
    }
  }

  // 图片预览
  .image-preview-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    overflow: hidden; // 防止页面滑动
    touch-action: none; // 防止触摸滑动
    -webkit-overflow-scrolling: none; // 防止iOS弹性滚动
    user-select: none; // 防止文本选择

    .image-preview-content {
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .preview-image {
      max-width: 90vw;
      max-height: 90vh;
    }

    .close-btn {
      position: absolute;
      top: -40px;
      right: -40px;
      width: 30px;
      height: 30px;
      background: #fff;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      font-size: 20px;
      color: #333;
    }
  }

  // 成功弹窗
  .success_box {
    width: 256px;
    height: 260px;
    font-size: 16px;
    color: #3d3d3d;
    img {
      width: 46px;
      height: auto;
      margin-bottom: 20px;
    }

    .suc_btn {
      width: 132px;
      height: 41px;
      color: #fff;
      font-size: 14px;
      background: #5463ff;
      margin-top: 30px;
      border-radius: 10px;
    }
  }

  // 成功弹窗
  :deep(.el-dialog__header) {
    padding: 0;
  }
  :deep(.el-dialog) {
    padding: 0;
    width: fit-content;
    border-radius: 13px;
  }
</style>
